package lession

import "fmt"

func SliceTest() {
	// Slice
	n := [6]int{3, 4, 5, 6, 7, 6}
	var o []int = n[1:4]
	fmt.Println(o)

	p := []int{5, 6, 4}
	fmt.Println(p)

	// Slice 代表的是指针，会影响到原数组的改变
	darr := [...]int{33, 55, 66, 77, 553, 55, 34, 78, 88, 5464}
	dslice := darr[2:5]
	fmt.Println("array before", darr)
	for index := range dslice {
		dslice[index]++
	}
	fmt.Println("array after", darr)

	numa := [3]int{2, 3, 5}
	nums1 := numa[:]
	nums2 := numa[:]
	fmt.Println("array before change1", numa)
	nums1[0] = 100
	fmt.Println("array after modification to slice nums1", numa)
	nums2[1] = 101
	fmt.Println("array after modification to slice nums2", numa)

	// Slice has length and capacity
	fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
	fruitslice := fruitarray[1:3]
	fmt.Printf("length of slice %d capacity %d\n", len(fruitslice), cap(fruitslice))

	// config capacity
	fruitslice = fruitslice[:4]
	fmt.Println("custom capacity", fruitslice)

	// re-slice upto capacity
	fruitslice = fruitslice[:cap(fruitslice)]
	fmt.Println("after re-slicing length is", len(fruitslice), " and capacity is", cap(fruitslice))
	fmt.Println("\nfruitslice", fruitslice)

	// make function: create an array and return a slice reference to it
	// make([]T, len, cap)-- 1. []T: type 2. len: length 3. cap: capacity
	demo1 := make([]int, 5, 5)
	fmt.Println(demo1)
}
